import { deepCopyObjArray, deepCopyObj } from '@/assets/js/help.js';
export default {
	data() {
		return {
			isFirstTimeToAdd: false, //是否第一次添加子封顶(自然日)
			isAddingSpecialRule: false,
			parkSpecialChargeVoList: {
				ruleList: []
			},
			specialRuleItemInitData: {
				isShow: true,
				chargeName: '',
				chargeType: 5,
				enableFlag: true,
				id: null,
				parkID: this.$route.query.parkId,
				parkSpecialChargeFreeVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					]
				},
				parkSpecialChargeAllDayTopVo: {
					//自然日(子封顶)
					institutionType: 0,
					parkSpecialChargeAllDayTopNaturalDayVo: {
						maxValue: '', //金额(最多小数点后两位)
						startTime: '', //时间(00:00:00)

						parkSpecialChargeAllDayTopNaturalDaySubTimeVoList: []
					},
					parkSpecialChargeAllDayTopParkingDurationVo: {
						maxValue: ''
					}
				},
				parkSpecialChargeAllDayTotalVo: {
					addUpType: 0,
					addUpValue: 0,
					institutionType: 0
				},
				parkSpecialChargeAmountPrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					prefrentialType: 0,
					prefrentialValue: 0
				},
				parkSpecialChargeTimePrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					parkingTimePrefrentialMinutes: 0
				},
				parkSpecialChargeServiceConditionVo: {
					serviceConditionType: '',
					serviceConditionValue: []
				},
				parkSpecialChargeRepeatShareFreeVo: {
					institutionType: 1,
					parkSpecialChargeRepeatShareFreeParkingDurationVo: null,
					parkSpecialChargeRepeatShareFreeNaturalDayVo: {
						startTime: null,
						freeValue: ''
					}
				}
			},
			specialRuleTestRules: {
				chargeName: [{ required: true, message: '请输入规则名称', trigger: 'blur' }],
				'parkSpecialChargeRepeatShareFreeVo.parkSpecialChargeRepeatShareFreeNaturalDayVo.freeValue': [
					{ required: true, message: '请输入免费时长', trigger: 'blur' }
				],
				'parkSpecialChargeServiceConditionVo.serviceConditionType': [
					{ required: true, message: '请选择使用条件', trigger: 'change' }
				],
				'parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo.maxValue': [
					{ required: true, message: '请输入封顶金额', trigger: 'blur' }
				]
			},
			specialRuleVipTypes: null,
			curEditSpRuleItem: null
		};
	},
	created() {
		if (this.specialRuleVipTypes === null) {
			this.getSpecialRuleVipTypes();
		}
		this.getParkSpecialChargeVoList();
	},
	methods: {
		getParkSpecialChargeVoList() {
			API.fetchGet('/operatorPark/getParkSpecialChargeVoList', {
				parkID: this.$route.query.parkId
			}).then(
				res => {
					if (res.data) {
						res.data.forEach(item => {
							for (let key in item) {
								if (item[key] === null) {
									item[key] = this.specialRuleItemInitData[key];
								}
							}

							item.isEdit = true;
							if (
								item.parkSpecialChargeServiceConditionVo.serviceConditionType == 3 ||
								item.parkSpecialChargeServiceConditionVo.serviceConditionType == 2
							) {
								item.parkSpecialChargeServiceConditionVo.serviceConditionValue = item.parkSpecialChargeServiceConditionVo.serviceConditionValue.split(
									','
								);
							}
							if (!item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo) {
								item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo = {
									maxValue: '', //金额(最多小数点后两位)
									startTime: '', //时间(00:00:00)
									parkSpecialChargeAllDayTopNaturalDaySubTimeVoList: []
								};
							}
							if (!item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo) {
								item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo = {
									maxValue: '' //金额(最多小数点后两位)
								};
							}
						});
						if (this.$route.query.specialRuleId) {
							this.parkSpecialChargeVoList.ruleList = res.data.filter(
								item => item.id == this.$route.query.specialRuleId
							);
						} else if (this.$route.query.isAddSpecialRule) {
							this.addSpecialRule();
						}
					}
				},
				err => {}
			);
		},
		getSpecialRuleVipTypes() {
			API.fetchPost('/operatorPark/selectVipTypeListBy', {
				parkId: this.$route.query.parkId,
				vipGroupType: 0
			}).then(res => {
				this.specialRuleVipTypes = res.data;
			});
		},
		setSpecialRuleInitData(val) {
			this.specialRuleItemInitData = {
				isShow: true,
				chargeName: '',
				chargeType: val ? val : 5,
				enableFlag: true,
				id: null,
				parkID: this.$route.query.parkId,
				parkSpecialChargeFreeVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					]
				},
				parkSpecialChargeAllDayTopVo: {
					institutionType: 0,
					parkSpecialChargeAllDayTopNaturalDayVo: {
						maxValue: '', //元(最多小数点后两位)
						startTime: '', //时间(00:00:00)
						parkSpecialChargeAllDayTopNaturalDaySubTimeVoList: []
					},
					parkSpecialChargeAllDayTopParkingDurationVo: {
						maxValue: ''
					}
				},
				parkSpecialChargeAllDayTotalVo: {
					addUpType: 0,
					addUpValue: 0,
					institutionType: 0
				},
				parkSpecialChargeAmountPrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					prefrentialType: 0,
					prefrentialValue: 0
				},
				parkSpecialChargeTimePrefrentialVo: {
					parkSpecialChargeDateTimeList: [
						{
							endTime: '',
							startTime: ''
						}
					],
					parkingTimePrefrentialMinutes: 0
				},
				parkSpecialChargeServiceConditionVo: {
					serviceConditionType: '',
					serviceConditionValue: []
				},
				parkSpecialChargeRepeatShareFreeVo: {
					institutionType: 1,
					parkSpecialChargeRepeatShareFreeParkingDurationVo: null,
					parkSpecialChargeRepeatShareFreeNaturalDayVo: {
						startTime: null,
						freeValue: ''
					}
				}
			};
		},
		addSpecialRule() {
			this.isAddingSpecialRule = true;
			this.setSpecialRuleInitData();
		},
		editSpItem(item) {
			this.curEditSpRuleItem = JSON.parse(JSON.stringify(item));
			if (!item.isEdit || item.isShow === false) {
				item.isShow = true;
				item.isEdit = true;
			} else {
				item.isEdit = false;
			}
		},
		cancelEditSpRule(list, index, item) {
			list.splice(
				index,
				1,
				Object.assign(this.curEditSpRuleItem, {
					isShow: true,
					isEdit: false
				})
			);
		},
		async SaveEditSpItem(list, index, item) {
			let vaild = await this.testSpecialListRule();

			if (vaild) {
				let itemObj = deepCopyObj(item);
				let saveData = JSON.parse(JSON.stringify(itemObj));
				if (Array.isArray(saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
					saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue = saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
						','
					);
				}
				// 24小时制-自然日
				if (saveData.chargeType === 1 && saveData.parkSpecialChargeAllDayTopVo.institutionType === 1) {
					let listTmp =
						saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
							.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
					if (this.testTimeRange24Hours(listTmp)) {
						this.showToast('封顶时段不能重合且组成24小时闭环', 0);
						return;
					}
					saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime = saveData
						.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
						? saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
								.length == 8
							? saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
							: saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime +
							  ':00'
						: '00:00:00';

					delete saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo;
				}
				// 24小时制-停车时长
				if (saveData.chargeType === 1 && saveData.parkSpecialChargeAllDayTopVo.institutionType === 0) {
					saveData.parkSpecialChargeAllDayTopVo.maxValue =
						saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo.maxValue;
					delete saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo;
				}

				API.fetchPost('/operatorPark/editParkSpecialChargeVo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('修改特殊收费规则成功', 1, 1500);
							this.back();
						} else {
							this.showToast(res.message, 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);
			}
		},
		testTimeRange24Hours(list) {
			let flag = false;
			if (list.length) {
				const day_rang = 24 * 60 * 60 * 1000;
				let cur_day_rang = 0;
				list.forEach(item => {
					item.startTime = item.startTime
						? item.startTime.length == 8
							? item.startTime
							: item.startTime + ':00'
						: '';
					item.endTime = item.endTime ? (item.endTime.length == 8 ? item.endTime : item.endTime + ':00') : '';
					let rang;
					if (this.getTime(item.startTime) >= this.getTime(item.endTime)) {
						rang = this.getTime(item.endTime) + day_rang - this.getTime(item.startTime);
					} else {
						rang = this.getTime(item.endTime) - this.getTime(item.startTime);
					}
					cur_day_rang += rang;
				});
				if (day_rang !== cur_day_rang) {
					flag = true;
				}
			}
			return flag;
		},
		getTime(time) {
			const d = new Date();
			return new Date([d.getFullYear(), d.getMonth() + 1, d.getDate()].join('/') + ' ' + time).getTime();
		},
		serviceConditionTypeChange(val) {
			if (val === 1) {
				this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
			}
			if (val === 2 || val === 3) {
				this.specialRuleItemInitData.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
			}
		},
		listItemServiceConditionTypeChange(val, item) {
			if (val === 1) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = '';
			}

			if (val === 3 || val === 2) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = [];
			}
		},
		deleteSubTopItem(item, index) {
			let list = this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
				.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
			if (list.length <= 2) {
				list.splice(0, list.length);
				this.isFirstTimeToAdd = false;
				return;
			}
			if (index == 0) {
				list[index + 1].startTime = item.startTime;
			} else if (index == list.length - 1) {
				list[index - 1].endTime = item.endTime;
			}
			list.splice(index, 1);
		},
		async addSpecialRuleToList() {
			let vaild = await this.testAddSpecialRule();

			if (vaild) {
				let saveData = JSON.parse(JSON.stringify(this.specialRuleItemInitData));
				if (Array.isArray(saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
					saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue = saveData.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
						','
					);
				}
				if (saveData.chargeType === 1 && saveData.parkSpecialChargeAllDayTopVo.institutionType === 1) {
					let listTmp =
						saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
							.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
					if (this.testTimeRange24Hours(listTmp)) {
						this.showToast('封顶时段不能重合且组成24小时闭环', 0);
						return;
					}
					saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime = saveData
						.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime
						? saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime + ':00'
						: '00:00:00';

					delete saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopParkingDurationVo;
				}

				if (saveData.chargeType === 1 && saveData.parkSpecialChargeAllDayTopVo.institutionType === 0) {
					delete saveData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo;
				}

				API.fetchPost('/operatorPark/addParkSpecialChargeVo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('添加特殊收费规则成功', 1, 1500);
							this.back();
						} else {
							this.showToast(res.message, 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);

				// this.parkSpecialChargeVoList.ruleList.push(this.specialRuleItemInitData);
			}
		},
		testAddSpecialRule() {
			return new Promise((resolve, reject) => {
				this.$refs['specialRuleItemInitData'].validate(valid => {
					if (valid) {
						resolve(true);
					} else {
						resolve(false);
					}
				});
			});
		},
		delSpecialRuleItem(list, index, item) {
			if (Array.isArray(item.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
				item.parkSpecialChargeServiceConditionVo.serviceConditionValue = item.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
					','
				);
			}
			const saveData = [item];
			API.fetchPost('/operatorPark/delParkSpecialChargeVo', saveData).then(
				res => {
					if (res.status === 1) {
						this.showToast('删除特殊收费规则成功', 1, 1500);
						list.splice(index, 1);
					} else {
						this.showToast(res.message, 2, 1500);
					}
				},
				err => {
					console.log(err);
				}
			);
		},
		//添加子封顶
		addNaturalDayTopItem() {
			if (this.isFirstTimeToAdd) {
				let list = this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo
					.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
				let obj = {
					startTime: '', //开始时间
					endTime: '', //结束时间
					maxValue: '' //金额(最多小数点后两位)
				};
				this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList.splice(
					list.length - 1,
					0,
					obj
				);
			} else {
				this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList.push(
					{
						startTime: '', //开始时间
						endTime: '', //结束时间
						maxValue: '' //金额(最多小数点后两位)
					},
					{
						startTime: '', //开始时间
						endTime: '', //结束时间
						maxValue: '' //金额(最多小数点后两位)
					}
				);
				if (
					this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
						.startTime
				) {
					this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[0].startTime = this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime;
					this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[1].endTime = this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime;
				}
			}
			this.isFirstTimeToAdd = this.isFirstTimeToAdd ? this.isFirstTimeToAdd : !this.isFirstTimeToAdd;
		},
		//自然日 起始时刻
		naturalDayStartTime(val) {
			let len = this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
				.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList.length;
			this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[0].startTime = val;
			this.specialRuleItemInitData.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList[
				len - 1
			].endTime = val;
		},
		// 自然日 起始时刻-单个规则
		naturalDayStartTimeForSingle(item) {
			let startTime = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime;
			let subTopList =
				item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
					.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
			subTopList[0].startTime = startTime;
			subTopList[subTopList.length - 1].endTime = startTime;
		},
		// 添加子封顶-单个规则
		addNaturalDayTopItemForSingle(list, index, item) {
			let obj = {
				startTime: '', //开始时间
				endTime: '', //结束时间
				maxValue: '' //金额(最多小数点后两位)
			};
			let subTopList =
				item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
					.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
			let startTimeVal = item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo.startTime;
			if (subTopList && Array.isArray(subTopList)) {
				if (subTopList.length >= 2) {
					subTopList.splice(subTopList.length - 1, 0, obj);
				} else {
					subTopList.push(
						{
							startTime: startTimeVal, //开始时间
							endTime: '', //结束时间
							maxValue: '' //金额(最多小数点后两位)
						},
						{
							startTime: '', //开始时间
							endTime: startTimeVal, //结束时间
							maxValue: '' //金额(最多小数点后两位)
						}
					);
				}
			}
		},
		// 删除子封顶-单个规则
		deleteSubTopItemForSingle(list, index, item, sindex) {
			let subTopList =
				item.parkSpecialChargeAllDayTopVo.parkSpecialChargeAllDayTopNaturalDayVo
					.parkSpecialChargeAllDayTopNaturalDaySubTimeVoList;
			if (subTopList && Array.isArray(subTopList)) {
				if (subTopList.length <= 2) {
					subTopList.splice(0, subTopList.length);
				} else {
					if (sindex == 0) {
						subTopList[sindex + 1].startTime = subTopList[sindex].startTime;
					} else if (sindex == subTopList.length - 1) {
						subTopList[sindex - 1].endTime = subTopList[sindex].endTime;
					}
					subTopList.splice(sindex, 1);
				}
			}
		}
	},
	watch: {
		'specialRuleItemInitData.chargeType'(val) {
			this.setSpecialRuleInitData(val);
		}
	}
};
